package com.mxx.common.thread;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

/**
 * @Author itzgyw
 * @Date 2020-01-22
 * @Time 17:55
 * @Description:
 */
@Slf4j
public class CountDownLatchHandleExecute {
    private ThreadHelper threadHelper;
    private CountDownLatch countDownLatch;

    public CountDownLatchHandleExecute(int count, ThreadHelper threadHelper) {
        this.threadHelper = threadHelper;
        this.countDownLatch = new CountDownLatch(count);
    }

    public CountDownLatch getCountDownLatch() {
        return countDownLatch;
    }

    public void exec(CountDownLatchHandle countDownLatchHandle) {
        countDownLatchHandle.setCountDownLatch(countDownLatch);
        threadHelper.exec(countDownLatchHandle);
    }

    public void exec(Runnable runnable) {
        CountDownLatchHandle countDownLatchHandle = new CountDownLatchHandle() {
            @Override
            public String getTag() {
                return "DefaultCountDownLatchHandleExecuteTag";
            }

            @Override
            public void exec() throws Exception {
                runnable.run();
            }

        };
        countDownLatchHandle.setCountDownLatch(countDownLatch);
        threadHelper.exec(countDownLatchHandle);
    }


    public void await() throws InterruptedException {
        countDownLatch.await();
    }


}
